/*********************************************************
 * copyright xinbohit.com 版权所有 
 * 开发人员：IvanYu
 * 创建时间：2014/9/12 10:13:55
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LEARNREN.COM.UI.LabelProviders
{
    class CTCLabel : AbstractLabelProvider,ILabelContainer
    {
        public CTCLabel(string label)
            : base(label)
        {

        }
        public override string GetHtml()
        {
            try
            {
                string[] paras = LabelProviderHelper.GetProperties(this.LabelString);
                if (paras.Length < 1)
                {
                    return this.LabelString;
                }

                // 栏目Id
                long cid = 0;
                if (!long.TryParse(paras[0], out cid))
                {
                    return this.LabelString;
                }

                // 获取内部所有标签并拼接结果
                string inner = LabelProviderHelper.GetInnerHtml(this.LabelString);

                if (string.IsNullOrEmpty(inner))
                {
                    return string.Empty;
                }

                TemplateContext tempC = new TemplateContext();
                tempC.CatalogueID = cid;
                tempC.Container = this;
                tempC.State = this.GroupLogicFactory.CatalogueLogic.GetByIdFromCache(cid);
                tempC.TemplateAbsoluteDirectory = this.Context.TemplateAbsoluteDirectory;
                tempC.TemplateVirtualDirectory = this.Context.TemplateVirtualDirectory;

                List<ILabelProvider> labels = LabelContenerHelper.GetLabels(inner, this.GroupLogicFactory, tempC);
                if (labels.Count > 0)
                {
                    StringBuilder sb = new StringBuilder(inner);
                    foreach (var label in labels)
                    {
                        string html=label.GetHtml();
                        if (html != label.LabelString)
                        {
                            sb.Replace(label.LabelString, html);
                        }
                    }
                    return sb.ToString();
                }
                else
                {
                    return inner;
                }
            }
            catch
            {
                return this.LabelString;
            }
        }
    }
}
